package com.neuedu.yyzx.controller;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.config.CommonResult;
import com.neuedu.yyzx.entity.Menu;
import com.neuedu.yyzx.service.IMenuService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author 第7组....
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    IMenuService umsPermissionService;
    @GetMapping("/list")
    CommonResult list(Menu menu, Page page1) {
        return CommonResult.success(umsPermissionService.list(menu,page1));
    }
    @PostMapping("/add")
    CommonResult add(Menu umsPermission) {

        if(umsPermissionService.add(umsPermission))
            return CommonResult.success("ok");
        else
            return CommonResult.failed("权限名已经存在");
    }
    @GetMapping("/getById")
    CommonResult getById(Integer id) {
        return CommonResult.success(umsPermissionService.getById(id));
    }
    @PostMapping("/update")
    CommonResult update(Menu umsPermission) {
        return CommonResult.success(umsPermissionService.updateById(umsPermission));
    }
    @PostMapping("/del")
    CommonResult del(Menu umsPermission) {
        umsPermission.setActive(0);
        return CommonResult.success(umsPermissionService.updateById(umsPermission));
    }

    @PostMapping("/back")
    CommonResult back(Menu umsPermission) {
        umsPermission.setActive(1);
        return CommonResult.success(umsPermissionService.updateById(umsPermission));
    }





    @PostMapping("/userPermissionList")
    CommonResult userPermissionList(HttpServletRequest request) {

        String token = request.getHeader("token");
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256("user")).build();
        DecodedJWT jwt = verifier.verify(token);
        Integer userId = jwt.getClaim("id").asInt();
        return CommonResult.success(umsPermissionService.userPermissionList(userId));
    }
}
